package main

import (
	"flag"
	"fmt"
	"io"
	"net"
	"os"
)

type Client struct {
	ServerIp   string
	ServerPort int
	Name       string
	conn       net.Conn
	flag       int //当前客户端的模式
}

const (
	EXIT = "exit"
)

func NewClient(serverIp string, serverPort int) *Client {
	client := &Client{
		ServerIp:   serverIp,
		ServerPort: serverPort,
		flag:       99,
	}
	//连接server
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort))
	if err != nil {
		fmt.Println("net.Dial err:", err)
		return nil
	}
	client.conn = conn
	return client
}

//DealResponsel 处理server回应的消息，直接显示到标准输出
func (c *Client) DealResponsel() {
	//一旦有数据就直接拷贝到标准输出 上，永久阻塞监听
	io.Copy(os.Stdout, c.conn)

}
func (c *Client) menu() bool {
	var flag int
	fmt.Println("1 公聊模式")
	fmt.Println("2 私聊模式")
	fmt.Println("3 更新用户名")
	fmt.Println("0 退出")
	fmt.Scanln(&flag)

	if flag >= 0 && flag <= 3 {
		c.flag = flag
		return true
	}
	fmt.Println(">>>请输入合法的数字<<<")
	return false
}

//PulicChat 公聊模式
func (c *Client) PulicChat() {
	var chatMsg string
	fmt.Println(">>>请输入内容，exit 退出")
	fmt.Scanln(&chatMsg)
	for chatMsg != EXIT {
		if len(chatMsg) != 0 {
			_, err := c.conn.Write([]byte(chatMsg))
			if err != nil {
				fmt.Println("conn write err:", err)
				break
			}
		}

		chatMsg = ""
		fmt.Println(">>>请输入内容，exit 退出")
		fmt.Scanln(&chatMsg)
	}
}
func (c *Client) GetAllOnLine() {
	_, err := c.conn.Write([]byte("who\n"))
	if err != nil {
		fmt.Println("conn Write err:", err)
		return
	}
}

// PrivateChat 私聊
func (c *Client) PrivateChat() {
	var remoteName, chatMsg string
	c.GetAllOnLine()
	fmt.Println(">>请输入聊天对象的用户名[用户名] exit退出")
	fmt.Scanln(&remoteName)

	for remoteName != EXIT {
		fmt.Println(">>请输入聊天消息 exit退出")
		fmt.Scanln(&chatMsg)
		for chatMsg != EXIT {
			if len(chatMsg) != 0 {
				sendMsg := fmt.Sprint("to|", remoteName, "|", chatMsg)
				fmt.Println(sendMsg)
				_, err := c.conn.Write([]byte(sendMsg + "\n"))
				if err != nil {
					fmt.Println("conn write err:", err)
					break
				}
			}
			fmt.Println(">>请输入聊天消息 exit退出")
			chatMsg = ""
			fmt.Scanln(&chatMsg)
		}
		fmt.Println(">>请输入聊天对象的用户名[用户名] exit退出")
		fmt.Scanln(&remoteName)
	}
}
func (c *Client) UpdateName() bool {
	//提示用户
	fmt.Println(">>>请出入用户名")
	fmt.Scanln(&c.Name)

	sendMsg := "rename|" + c.Name + "\n"
	_, err := c.conn.Write([]byte(sendMsg))
	if err != nil {
		fmt.Println("conn.Write err", err)
		return false
	}
	return true
}
func (c *Client) Run() {
	for c.flag != 0 {
		for !c.menu() {
			return
		}
		//根据不同模式处理不同业务
		switch c.flag {
		case 1:
			//公聊
			fmt.Println("公聊")
			c.PulicChat()
			break
		case 2:
			fmt.Println("私聊模式")
			c.PrivateChat()
			break
		case 3:
			//更新用户名
			c.UpdateName()
			fmt.Println("更新用户名")
			break

		}
	}
}

var (
	serverIp   string
	serverPort int
)

func init() {
	flag.StringVar(&serverIp, "ip", "127.0.0.1", "设置服务器IP地址(默认是127.0.0.1)")
	flag.IntVar(&serverPort, "port", 8880, "设置服务器默认端口(默认是8880)")
}

//go build -o client.exe ./
func main() {

	//命令行解析
	flag.Parse()

	client := NewClient(serverIp, serverPort)
	if client == nil {
		fmt.Println(">>>连接服务器失败<<<")
		return
	}
	//单独开启一个goroutine去处理server的回执消息
	go client.DealResponsel()

	fmt.Println(">>>>连接成功<<<")
	client.Run()
}
